﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.IO;
using MPMovieCatalog.Domain;

namespace MPMovieCatalogServer.Server
{
    class Worker
    {
        private HttpListenerContext context;

        public Worker(HttpListenerContext context)
        {
            this.context = context;
        }

        public void ProcessRequest()
        {
            /*
            string msg = context.Request.HttpMethod + " " + context.Request.Url;
            Console.WriteLine(msg);

            StringBuilder sb = new StringBuilder();

            string command = context.Request.QueryString.Get("command");
            if (command == "findAllMovies")
            {
                XmlDocument moviesXML = new XmlDocument();
                XmlElement root = moviesXML.CreateElement("movies");
                moviesXML.AppendChild(root);

                foreach (MovieShortInfo movie in MovieDAO.FindAllMoviesShortInfo())
                {
                    root.AppendChild(movie.ToXMLElement(moviesXML));
                }

                using (XmlWriter writer = XmlTextWriter.Create(sb))
                {
                    writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");                    //writer.Settings.Encoding = Encoding.UTF8;
                    moviesXML.WriteTo(writer);
                }
            }

            byte[] b = Encoding.UTF8.GetBytes(sb.ToString());
            context.Response.ContentLength64 = b.Length;
            context.Response.ContentType = "text/xml";
            context.Response.OutputStream.Write(b, 0, b.Length);
            context.Response.OutputStream.Close();
            */ 
        }
    }
}
